﻿using System;
using System.Data;
using System.Linq;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.UI;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI;
using System.Web;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.Share;
using LMHTX.Modules.DIRECTORY.TEAM.Components;

namespace LMHTX.Modules.DIRECTORY.TEAM.Controls
{
    public partial class AreaManager : AddEditItemControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["AreaID"] = "";
                ViewState["dtArea"] = null;
                LoadData();
            }
        }

        public void LoadData()
        {
            DataTable dt = Components.Area.SelectAll(this.ModuleID).Tables[0];
            gvArea.DataSource = dt;
            gvArea.DataBind();
            ViewState["dtArea"] = dt;
        }     

        protected void gvArea_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Components.Area.DeleteArea(Convert.ToInt32(gvArea.DataKeys[e.RowIndex].Value));
            LoadData();
        }

        protected void gvArea_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvArea.PageIndex = e.NewPageIndex;
            LoadData();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Components.Area area = new Components.Area();
            area.Title = txtTitle.Text;
            area.Description = txtDes.Text;
            area.ModuleID = this.ModuleID;
            if (ViewState["AreaID"].ToString().Trim().Length != 0)
            {
                if (Int32.Parse(ViewState["AreaID"].ToString()) != 0)
                {
                    area.ID = Int32.Parse(ViewState["AreaID"].ToString());
                    area.Update();
                }
            }
            else
            {
                area.Insert();
            }
            LoadData();
            Reset();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Reset();
        }

        public void Reset()
        {
            txtTitle.Text = "";
            txtDes.Text = "";
            ViewState["AreaID"] = "";
            gvArea.SelectedIndex = -1;
        }

        protected void gvArea_SelectedIndexChanged(object sender, EventArgs e)
        {
            string key = gvArea.SelectedDataKey.Value.ToString();

            ViewState["AreaID"] = key;
            if (ViewState["dtArea"] != null)
            {
                DataTable dt = (DataTable)ViewState["dtArea"];
                DataRow[] dr = dt.Select("ID= " + key);
                txtTitle.Text = dr[0]["Title"].ToString();
                txtDes.Text = dr[0]["Description"].ToString();

            }
        }
    }
}